package cn.smbms.controller;

import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.smbms.pojo.Provider;
import cn.smbms.pojo.Role;
import cn.smbms.service.provider.ProviderService;
import cn.smbms.tools.Constants;
import cn.smbms.tools.PageSupport;

import com.alibaba.fastjson.JSONArray;

@Controller
@RequestMapping("/provider")
public class ProviderController {
	@Resource
	private ProviderService providerService;

	@RequestMapping(value = "/add.html", method = RequestMethod.GET)
	public String providerAdd(@ModelAttribute("provider") Provider provider) {
		return "provideradd";

	}

	@RequestMapping(value = "/providermodifysave", method = RequestMethod.POST)
	public String modifyproviderSave(Provider provider, HttpSession session)
			throws Exception {
		System.out.println(provider.getId());
		if (providerService.modify(provider) > 0) {
			return "redirect:/provider/providerList.html";
		}
		return "redirect:/provider/providerList.html";
	}

	@RequestMapping(value = "/delProviderController")
	@ResponseBody
	public Object deluser(@RequestParam int proid) throws Exception {
		int ri = providerService.deleteUserById(proid);
		HashMap<String, String> resultMap = new HashMap<String, String>();

		if (ri > 0) {
			resultMap.put("delResult", "true");
		} else {
			resultMap.put("delResult", "false");
		}
		return JSONArray.toJSONString(resultMap);
	}

	@RequestMapping(value = "/view/{id}", method = RequestMethod.GET)
	public String view(@PathVariable String id, Model model) {
		Provider provider = providerService.getProviderById(id);
		model.addAttribute(provider);
		return "providerview";
	}

	@RequestMapping(value = "/providermodify.html", method = RequestMethod.GET)
	public String getUserById(@RequestParam String uid, Model model) {
		Provider provider = providerService.getProviderById(uid);
		model.addAttribute(provider);
		return "providermodify";

	}

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public String addprovider(@Valid Provider provider) throws Exception {

		if (providerService.add(provider) > 0) {
			return "redirect:/provider/providerList.html";
		}
		return "provideradd";

	}

	@RequestMapping(value = "/providerList.html")
	public String getProviderList(
			Model model,
			@RequestParam(value = "queryProName", required = false) String queryUserName,
			@RequestParam(value = "queryProCode", required = false) String queryUserRole,
			@RequestParam(value = "pageIndex", required = false) String pageIndex) {
		int _queryUserRole = 0;
		List<Provider> providerLiset = null;
		int pageSize = Constants.pageSize;
		int currentPageNo = 1;
		if (queryUserName == null) {
			queryUserName = "";
		}
		if (queryUserRole != null && !queryUserRole.equals("")) {
			_queryUserRole = Integer.parseInt(queryUserRole);
		}

		try {
			currentPageNo = Integer.valueOf(pageSize);
		} catch (NumberFormatException e) {
			return "redirect:/user/syserror.html";
		}

		int totalConut = providerService.getProviderList(queryUserName,
				queryUserRole).size();
		currentPageNo = 0;
		PageSupport pages = new PageSupport();
		pages.setCurrentPageNo(currentPageNo);
		pages.setPageSize(pageSize);
		pages.setTotalPageCount(totalConut);
		int totalPageCont = pages.getTotalPageCount();
		if (currentPageNo < 1) {
			currentPageNo = 1;
		} else if (currentPageNo > totalPageCont) {
			currentPageNo = totalPageCont;
		}
		providerLiset = providerService.getProviderList(queryUserName,
				queryUserRole);
		model.addAttribute("providerList", providerLiset);
		List<Role> rolelist = null;
		model.addAttribute("roleList", rolelist);
		model.addAttribute("queryUserName", queryUserName);
		model.addAttribute("queryUserRole", queryUserRole);
		model.addAttribute("totalpageCount", totalPageCont);
		model.addAttribute("currentPageNo", currentPageNo);

		return "providerlist";
	}

}
